package com.smartstate.dataManagement.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.dataManagement.dto.AlarmDTO;
import com.smartstate.dataManagement.dto.PageDTO;
import com.smartstate.dataManagement.entity.Alarm;
import com.smartstate.dataManagement.mapper.AlarmMapper;
import com.smartstate.dataManagement.mapper.ObjMoudelMapper;
import com.smartstate.dataManagement.service.AlarmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Transactional
@Service("alarmService")
@Slf4j
public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements AlarmService {

    @Resource
    private ObjMoudelMapper objMoudelMapper;

    private Map<String, Object> objectMap = null;

    private Map<String, Object> deviceIdMap = null;

    /**
     * 分页查询设备报警
     *
     * @param pageDTO
     * @return
     */
    @Override
    public Page<Alarm> queryAllPageByDTO(PageDTO pageDTO) {
        //创建查询语句包装器和分页查询表对象
        Page<Alarm> page = new Page(pageDTO.getPage(), pageDTO.getSize());
        QueryWrapper<Alarm> queryWrapper = new QueryWrapper<>();
        if (pageDTO.getDto()!= null) {
            //条件参数
            AlarmDTO alarmDTO = (AlarmDTO) pageDTO.getDto();

            //楼层区域条件
            if (alarmDTO.getObjId() != null && !"".equals(alarmDTO.getObjId())) {
                queryWrapper.and(wrapper -> wrapper.eq("objId", alarmDTO.getObjId()));
            }

            //设备编码条件
            if (alarmDTO.getDeviceId() != null && !"".equals(alarmDTO.getDeviceId())) {
                queryWrapper.and(wrapper -> wrapper.like("deviceId", alarmDTO.getDeviceId()));
            }

            //设备名称条件
            if (alarmDTO.getDeviceName() != null && !"".equals(alarmDTO.getDeviceName())){
                queryWrapper.and(wrapper -> wrapper.like("eName", alarmDTO.getDeviceName()));
            }

            //设备属性条件
            if (alarmDTO.getAttributes() != null && !"".equals(alarmDTO.getAttributes())){
                queryWrapper.and(wrapper -> wrapper.eq("attributes", alarmDTO.getAttributes()));
            }

            //设备是否在线
            if (alarmDTO.getType()!= null && !"".equals(alarmDTO.getType())){
                queryWrapper.and(wrapper -> wrapper.eq("type", alarmDTO.getType()));
            }



            //报警时间
            if (alarmDTO.getAbnormaltime() != null && !"".equals(alarmDTO.getAbnormaltime()) && alarmDTO.getAbnormaltimeEnd() != null && !"".equals(alarmDTO.getAbnormaltimeEnd())){
                queryWrapper.and(wrapper -> wrapper.ge("abnormaltime",alarmDTO.getAbnormaltime()).le("abnormaltime",alarmDTO.getAbnormaltimeEnd()));
            }

            //处理完成时间
            if (alarmDTO.getStopTime() != null && !"".equals(alarmDTO.getStopTime()) && alarmDTO.getStopTimeEnd() != null  && !"".equals(alarmDTO.getStopTimeEnd())){
                queryWrapper.and(wrapper -> wrapper.ge("stopTime",alarmDTO.getStopTime()).le("stopTime",alarmDTO.getStopTimeEnd()));
            }
        }

        queryWrapper.orderByDesc("abnormaltime");

        Page<Alarm> alarmPage = baseMapper.selectPage(page, queryWrapper);


        //----------------------------------------报警楼层名称拼接--------------------------------------------------------------

        //查询空间结构中的所有objId
        objectMap = objMoudelMapper.queryAllObjId("1");
        //查询所有设备对应空间建模的所有objId
        deviceIdMap = objMoudelMapper.queryAllDeviceIdFitAllObjId("1");

        for (Alarm record : alarmPage.getRecords()) {
            //进行名称拼接
            String objId  = String.valueOf(((Map) deviceIdMap.get(record.getDeviceId())).get("objId"));
            Map<String, Object> map = (Map) objectMap.get(objId);
            String objIdName = "";
            while(true){
                objIdName = objIdName == "" ? map.get("name") + objIdName : map.get("name") + "-" + objIdName;
                if ("0".equals(map.get("parentId"))){
                    break;
                }
                map = (Map) objectMap.get(map.get("parentId"));
            }
            record.setObjName(objIdName);
        }

        return alarmPage;
    }


    /**
     * 处理完成
     * @param alarm
     * @return
     */
    @Override
    public Boolean figureOut(Alarm alarm) {
        if (alarm.getState()!= null && !"".equals(alarm.getState()) && alarm.getState().equals("1")){
            Date dates = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");
            String format = dateFormat.format(dates);
            alarm.setStopTime(format);
        }
        return this.saveOrUpdate(alarm);
    }
}
